package com.xhorse.lockplatform.manage.controller;

import com.xhorse.lockplatform.comm.constant.FilePathDefine;
import com.xhorse.lockplatform.service.ChipDataImportService;
import com.xhorse.lockplatform.service.JpushService;
import com.xhorse.lockplatform.service.RemoteDataImportService;
import com.xhorse.lockplatform.service.RenewDataImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("DataImport")
public class DataImportController {

    @Autowired
    private RemoteDataImportService dataImportService;
    @Autowired
    private ChipDataImportService chipDataImportService;
    @Autowired
    private RenewDataImportService renewDataImportService;

    @Autowired
    private JpushService jpushService;

    private static ReentrantLock remotelock = new ReentrantLock();

    private static ReentrantLock chiplock = new ReentrantLock();

    private static ReentrantLock renewlock = new ReentrantLock();

    @RequestMapping("/remote")
    public String remote() {
        return "DataImport/remote";
    }
    @RequestMapping("/chip")
    public String chip() {
        return "DataImport/chip";
    }
    @RequestMapping("/renew")
    public String renew() {
        return "DataImport/renew";
    }

    /**
     * 导入遥控生成数据
     *
     * @param regionId 根据地区来划分
     */
    @RequestMapping(value = "/ImportRemoteData", method = RequestMethod.POST)
    public String RemoteDataImport(Model model, MultipartFile multipartFile,
                                   int regionId) {
        if (remotelock.isLocked()) {
            model.addAttribute("Msg", "1003");
        } else {
            try {
                remotelock.lock();
                String desPath = FilePathDefine.ZIP_DOWNLOAD_PATH + regionId
                        + ".zip";
                Thread.sleep(3000);
                downloadFile(multipartFile, desPath);
                dataImportService.dataImport(regionId);
                dataImportService.zipAllGenerate(regionId, "0");
                model.addAttribute("Msg", "1001");
            } catch (Exception e) {
                model.addAttribute("Msg", "1002");
            } finally {
                remotelock.unlock();
            }
            // 发送推送消息
            jpushService.pushUpdateInfo(regionId);
        }
        return "DataImport/remote";
    }

    /**
     * 导入芯片生成数据
     *
     * @param model
     * @param multipartFile
     * @param regionId
     * @return
     */
    @RequestMapping(value = "/ChipDataImport", method = RequestMethod.POST)
    public String ChipDataImport(Model model, MultipartFile multipartFile,
                                 int regionId) {
        if (!chiplock.isLocked()) {
            try {
                chiplock.lock();
                String desPath = FilePathDefine.CHIP_DATABASE + "chip_"
                        + regionId + ".json";
                downloadFile(multipartFile, desPath);
                chipDataImportService.ChipDataImport(regionId);
                model.addAttribute("Msg", 1001);
            } catch (Exception e) {
                model.addAttribute("Msg", 1002);
            } finally {
                chiplock.unlock();
            }
        } else {
            model.addAttribute("Msg", 1003);
        }
        return "DataImport/chip";
    }

    /**
     * 导入钥匙解锁数据
     * @param model
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/RenewDataImport", method = RequestMethod.POST)
    public String RenewDataImport(Model model, MultipartFile multipartFile) {
        if (!renewlock.isLocked()) {
            try {
                renewlock.lock();
                String desPath = FilePathDefine.ZIP_DOWNLOAD_PATH + "renew.zip";
                downloadFile(multipartFile, desPath);
                renewDataImportService.RenewImport();
                model.addAttribute("Msg", 1001);
            } catch (Exception e) {
                model.addAttribute("Msg", 1002);
            } finally {
                renewlock.unlock();
            }
        } else {
            model.addAttribute("Msg", 1003);
        }
        return "DataImport/renew";
    }


    /**
     * 下载方法
     *
     * @param url        下载地址
     * @param importType 1：遥控生成，2：芯片生成，3：钥匙解锁
     */
    public void downloadFile(MultipartFile multipartFile, String desPath) {
        try {
            // 获取文件名称作为保存到服务器的文件名称
            String filename = multipartFile.getOriginalFilename();
            if (filename != "") {
                // 前半部分路径，目录，将WebContent下的一个名称为images文件夹转换成绝对路径
                File file = new File(desPath);
                multipartFile.transferTo(file);
            }
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
    }
}
